from utils import get_agg_trades, df_into_db, db_config
import pandas as pd
import pymysql


def get_exist_data(symbol):
    conn = pymysql.connect(db='binance', **db_config)
    sql = (f"select * from agg_trades where symbol = '{symbol}' "
           f"and `T` = (select min(`T`) from agg_trades where symbol = '{symbol}')")
    df = pd.read_sql(sql, conn)
    return df


for _ in range(100):
    symbol = "BTCUSDT"
    exist_data = get_exist_data(symbol)
    limit = 1000
    first_time = exist_data.iloc[0]['T']
    result_json = get_agg_trades(symbol, limit, first_time)
    result_df = pd.DataFrame(result_json)
    result_df = result_df[~result_df['a'].isin(set(exist_data['a']))]
    result_df['symbol'] = symbol
    df_into_db(result_df, "agg_trades")
